package com.ericsson.eritask.dao.impl;

import java.util.ArrayList;
import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import javax.persistence.TypedQuery;

import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import com.ericsson.eritask.dao.UserProjectDao;
import com.ericsson.eritask.domain.User;
import com.ericsson.eritask.domain.UserProject;

@Repository
public class UserProjectDaoImpl implements UserProjectDao{

	@PersistenceContext
	EntityManager entityManager;

	
	@Transactional
	@Override
	public List<UserProject> getUserProjectDtls(String user_name, String password) {
		// TODO Auto-generated method stub
		
		List<User> userList = null;
		List<UserProject> userProjectList = null;
		try{
		String signumId = null;	
		// user_name ="1234";	
		//password="sushanta";
		TypedQuery<User> query = entityManager.createQuery("select u from User u where u.userName = ?1 and u.password =?2",User.class );
	
		query.setParameter(1,user_name );
		query.setParameter(2,password );
		 userList = query.getResultList();
		 System.out.println("userList:"+userList.size());
	    if(userList!=null && userList.size()>0){
		for(User user:userList){
			System.out.println("signum:"+user.getSignum());
			//System.out.println("project_id from database:"+project_id);	
			signumId = user.getSignum();
		}
		TypedQuery<UserProject> query11 = entityManager.createQuery("select u from UserProject u where u.user.signum = ?1 ",UserProject.class );
		query11.setParameter(1,signumId );
		userProjectList = query11.getResultList();
		System.out.println("userProjectList:"+userProjectList.size());
		for(UserProject  userproject:userProjectList ){
			System.out.println("userproject id :"+userproject.getProject().getProjectId());
		}
	   }
	  
	}
	   catch(Exception e){
		   e.printStackTrace();
	   }
		   
		return userProjectList;
	}


	@Transactional
	@Override
	public List<String> getAllUsers(int projectId) {
		
System.out.println("in userprojectDAO  getAllUsers method projectid::::::::::"+projectId);
		List<String> ulist= new ArrayList<String>();
		List<UserProject> userProjects;
		
		Query query = entityManager.createQuery("select u from UserProject u where u.project.projectId= :projectId",UserProject.class);
		query.setParameter("projectId", projectId);
		userProjects = query.getResultList();
		
		for(UserProject us :userProjects){
			System.out.println("User Signum:::::::::"+us.getUser().getSignum());
			
			String signum = us.getUser().getSignum();
			
			ulist.add(signum);
		}
		System.out.println("UserList size ::::::::::::"+ulist);
		return ulist;
	}

		
	
	

}
